package com.naizhuai.ui.task;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.naizhuai.MainActivity;
import com.naizhuai.Model.AccountHelper;
import com.naizhuai.R;
import com.naizhuai.ui.BaseFragment;
import com.naizhuai.ui.components.MultStateView;
import com.naizhuai.ui.entity.Bus.Bus;
import com.naizhuai.ui.mianbusiness.action.ActionMainLazyFragment;
import com.naizhuai.ui.mianbusiness.target.TargetMainLazyFragment;
import com.naizhuai.util.ActivityStackManager;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.RxBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.functions.Action1;


public class ActionFragment extends BaseFragment {
    @BindView(R.id.task_tab)
    TabLayout tabLayout;
    @BindView(R.id.task_pager)
    ViewPager viewPager;
    @BindView(R.id.container_State)
    MultStateView ContainerState;
    Unbinder unbinder;
    private List<String> mTitles;
    private  List<Fragment> fragmentList;
    public ActionFragment() {
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        NaizhuAILog.d("onAttach");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        NaizhuAILog.d("onDetach");
    }
    public static ActionFragment newInstance() {
        ActionFragment fragment = new ActionFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_action, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        accordingToPowerLoadView(true);

    }

    private void accordingToPowerLoadView(boolean isinit) {
        if (TextUtils.isEmpty(AccountHelper.getComPanyid())){
            ContainerState.showView(MultStateView.STATUS_NOPOWER);
        }else {
            ContainerState.showView(MultStateView.STATUS_CONTENT);
            if (isinit){
                initFragment();
                initAdapter();
                tabLayout.setupWithViewPager(viewPager);
            }

        }
    }

    private  boolean isReceive;


    @Override
    public void onPause() {
        super.onPause();
        isReceive=false;
    }

    @Override
    public void onResume() {
        super.onResume();
        NaizhuAILog.d("onResumeAction");
//        accordingToPowerLoadView(false);
        isReceive=true;
        RxBus.getDefault().toObservableSticky(Bus.UserChange.class)
                .subscribe(new Action1<Bus.UserChange>() {
                    @Override
                    public void call(Bus.UserChange userChange) {
                        if (isReceive){
                            decrementcount();
                            accordingToPowerLoadView(true);
                            if (getcount()==0){
                                RxBus.getDefault().removeStickEvent(Bus.UserChange.class);
                                reset();
                            }
                        }
                    }
                });
    }
    private void initAdapter() {
        FragmentPagerAdapter pageAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles.get(position);
            }

        };
        viewPager.setAdapter(pageAdapter);
        viewPager.setOffscreenPageLimit(3);
    }


    private void initFragment() {
        mTitles= Arrays.asList(getResources().getStringArray(R.array.action_string));
        fragmentList = new ArrayList();
        fragmentList.add(ActionMainLazyFragment.newInstance());
        fragmentList.add(TaskFragment.newInstance());
        fragmentList.add(TargetMainLazyFragment.newInstance());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
